/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Form;

import BLL.BLLUser;
import DTO.DTOUser;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pham
 */
public class JPanelManageUser extends javax.swing.JPanel {
    BLLUser bllUser;
    DefaultTableModel modelUser;
    /**
     * Creates new form JPanelManageUser
     */
    public JPanelManageUser() {
        bllUser = new BLLUser();
        initComponents();
        modelUser = new DefaultTableModel();
        String[] cols = {"Mã", "Tên đăng nhập", "Quyền truy cập", "Họ tên", "Giới tính","Địa chỉ", "Email", "Số điện thoại", "Ngày tạo tài khoản"};
        modelUser.setColumnIdentifiers(cols);
        tbUser.setModel(modelUser);
        loadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbUser = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        btnAddNew = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDel = new javax.swing.JButton();

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách người dùng"));

        tbUser.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbUser);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnAddNew.setText("Thêm người dùng");
        btnAddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNewActionPerformed(evt);
            }
        });

        btnEdit.setText("Sửa người dùng");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDel.setText("Xóa người dùng");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAddNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEdit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddNew)
                    .addComponent(btnEdit)
                    .addComponent(btnDel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1159, Short.MAX_VALUE)
                        .addGap(31, 31, 31))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(143, 143, 143))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNewActionPerformed
        try{
            JDialogUser dialogUser = new JDialogUser(null, true, -1);
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            dialogUser.setLocation((dim.width - dialogUser.getWidth())/2, (dim.height - dialogUser.getHeight())/2);
            dialogUser.addWindowListener(new WindowListener() {

                    @Override
                    public void windowOpened(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowClosing(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowClosed(WindowEvent e) {
                        loadData();
                    }

                    @Override
                    public void windowIconified(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowDeiconified(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowActivated(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowDeactivated(WindowEvent e) {
                        
                    }
                });
            dialogUser.setVisible(true);
        }
        catch(HeadlessException ex)
        {
            JOptionPane.showMessageDialog(null, "lỗi");
        }
    }//GEN-LAST:event_btnAddNewActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
       try{
            int row = tbUser.getSelectedRow();
            if(row > -1)
            {
                int id = (int)tbUser.getValueAt(row, 0);
                JDialogUser dialogUser = new JDialogUser(null, true, id);
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                dialogUser.setLocation((dim.width - dialogUser.getWidth())/2, (dim.height - dialogUser.getHeight())/2);
                dialogUser.addWindowListener(new WindowListener() {

                    @Override
                    public void windowOpened(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowClosing(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowClosed(WindowEvent e) {
                        loadData();
                    }

                    @Override
                    public void windowIconified(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowDeiconified(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowActivated(WindowEvent e) {
                        
                    }

                    @Override
                    public void windowDeactivated(WindowEvent e) {
                        
                    }
                });
                dialogUser.setVisible(true);
            }
            else
                JOptionPane.showMessageDialog(null, "Vui lòng chọn người dùng cần sửa");
        }
        catch(HeadlessException ex)
        {
            JOptionPane.showMessageDialog(null, "lỗi");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        try{
            int[] rows = tbUser.getSelectedRows();
            if(rows.length > 0)
            {
                int confirm = JOptionPane.showConfirmDialog(null, "Bạn có chắc muốn xóa?", "Cảnh báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if(confirm==JOptionPane.YES_OPTION)
                {
                    modelUser = (DefaultTableModel)tbUser.getModel();
                    for(int i = rows.length - 1; i >= 0; i--)
                    {
                        int id = (int)tbUser.getValueAt(rows[i], 0);
                        DTOUser dtoUser = bllUser.getUserById(id);
                        if(JDialogLogin.usr.equals(dtoUser.getUserName()) || "admin".equals(dtoUser.getUserName()))
                        {
                            JOptionPane.showMessageDialog(null, "Không thể xóa");
                        }
                        else{
                            if(bllUser.deleteUser(id))
                                modelUser.removeRow(rows[i]);
                        }
                    }
                }
            }
            else
                JOptionPane.showMessageDialog(null, "Vui lòng chọn người dùng cần xóa");
        }
        catch(HeadlessException ex)
        {
            JOptionPane.showMessageDialog(null, "không thể xóa người dùng");
        }
    }//GEN-LAST:event_btnDelActionPerformed

    //load data to table
    private void loadData()
    {
        modelUser = (DefaultTableModel)tbUser.getModel();
        for(int i = tbUser.getRowCount() - 1; i >= 0; i--)
        {
            modelUser.removeRow(i);
        }
        for(DTOUser dtoUser : bllUser.getAllUser())
        {
            modelUser.addRow(new Object[]{dtoUser.getUserId(), dtoUser.getUserName(), dtoUser.getRole(), dtoUser.getUserFullName(), dtoUser.getUserGender(), dtoUser.getUserAddress(),
                                            dtoUser.getUserEmail(), dtoUser.getUserPhone(), dtoUser.getUserDateCreated()});
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddNew;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnEdit;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbUser;
    // End of variables declaration//GEN-END:variables
}
